package com.copro.chemdb.stat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.ihelpuoo.lang.NumericUtils;

import com.copro.chemdb.stat.rule.AccumulateStatResult;
import com.copro.chemdb.subject.Subject;
import com.copro.chemdb.subject.SubjectUtils;
import com.copro.simple.Application;

public class ShowStatAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Application app = Application.me(request);
		Subject subject = SubjectUtils.get(app, ((Subject) form));
		int statType = NumericUtils.parseInt(request.getParameter("statType"),
				1);
		if (subject.isHasOptions()) {
			subject.setOptions(SubjectUtils.queryOptions(app, subject));
		}
		StatRule rule = app.getStatRule(subject.getRule());
		StatResult re = rule.doStat(app, subject, statType);

		if (re instanceof AccumulateStatResult) {
			((AccumulateStatResult) re).formatNameToPath(mapping);
			request.setAttribute("list", re.getResult());
		} else
			request.setAttribute("result", re.getResult());
		return mapping.findForward(re.getForwardName());
	}

}
